﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.DataAccess;
using System.Collections.ObjectModel;
using University.Workshop.Model;
using System.Net.Sockets;
namespace University.Workshop.ServerApp
{
    /// <summary>
    /// Interaction logic for Banned.xaml
    /// </summary>
    public partial class Banned : Window
    {
        DatabaseQuery data = new DatabaseQuery();
        ASCIIEncoding encoder = new ASCIIEncoding();
        int actualDay = 0;
        int actualHour = 0;
        int actualMinute = 0;
        int actualSecond = 0;
        
        public Banned()
        {
            InitializeComponent();
            foreach (User user in data.GetAllUsers)
            {
                listBox1.Items.Add(user.Name);
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(listBox1.SelectedIndex>-1)
            textBox2.Text = listBox1.SelectedItem.ToString();
        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                
                int days = Convert.ToInt32(textBox1.Text);
                int hours = Convert.ToInt32(textBox3.Text); ;
                int minutes = Convert.ToInt32(textBox4.Text); ;
                int seconds = Convert.ToInt32(textBox5.Text); ;
                if (days+hours+minutes+seconds > 0 )
                {
                    if (textBox2.Text != "")
                    {
                        MessageBoxResult result = MessageBox.Show("Are you sure you want to ban " + textBox2.Text, "Are you sure?", MessageBoxButton.YesNo);
                         if (result == MessageBoxResult.Yes)
                         {
                             if (data.BanUser(textBox2.Text, "True", DateTime.Now.AddDays(days).AddHours(hours).AddMinutes(minutes).AddSeconds(seconds)))
                             {
                                 NetworkStream stream = BannedUsers.Instance.GetStream(textBox2.Text);
                                 if (stream != null)
                                 {
                                     byte[] buffer = encoder.GetBytes("You are Banned,you can return on " + DateTime.Now.AddDays(days).AddHours(hours).AddMinutes(minutes).AddSeconds(seconds));
                                     stream.Write(buffer, 0, buffer.Length);
                                 }
                                 MessageBox.Show("user banned");
                                 listBox1.Items.Remove(textBox2.Text);
                                 listBox1.SelectedIndex = -1;
                                 textBox2.Text = "";
                                 textBox1.Text = "0";
                                 textBox3.Text = "0";
                                 textBox4.Text = "0";
                                 textBox5.Text = "0";
                             }
                             else
                                 MessageBox.Show("Error");
                         }
                    }
                    else
                        MessageBox.Show("Please select a user");
                }
                else
                    MessageBox.Show("The number must be greater than zero");
            }
            catch (Exception)
            {
                MessageBox.Show("Please insert a valid day");
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            listBox1.Items.Clear();
            foreach (User user in data.GetAllUsers)
            {
                listBox1.Items.Add(user.Name);
            }

        }

        private void textBox5_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                int Value = Convert.ToInt32(textBox5.Text);
                if (textBox5.Text.Contains("+") || textBox5.Text.Contains("-"))
                    textBox5.Text = actualSecond.ToString();
                if (Value < 0)
                    Value *= -1;
                actualSecond = Value;

                if (Value > 59)
                {
                    Value = 59;
                    actualSecond = 59;
                    textBox5.Text = "59";
                }
                
            }
            catch (FormatException)
            {
                if (textBox5.Text != "")
                {
                    if (textBox5.Text.Length > 1)
                        textBox5.Text = actualSecond.ToString();
                    else
                    {
                        textBox5.Text = "";
                    }
                }
                
            }
        }

        private void textBox4_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                int Value = Convert.ToInt32(textBox4.Text);
                if (textBox4.Text.Contains("+") || textBox4.Text.Contains("-"))
                    textBox4.Text = actualMinute.ToString();
                if(Value < 0)
                    Value *= -1;
                
                actualMinute = Value;

                if (Value > 59)
                {
                    Value = 59;
                    actualMinute = 59;
                    textBox4.Text = "59";
                }

            }
            catch (FormatException)
            {
                if (textBox4.Text != "")
                {
                    if (textBox4.Text.Length > 1)
                        textBox4.Text = actualMinute.ToString();
                    else
                    {
                        textBox4.Text = "";
                    }
                }

            }
        }

        private void textBox3_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                int Value = Convert.ToInt32(textBox3.Text);
                if (textBox3.Text.Contains("+") || textBox3.Text.Contains("-"))
                    textBox3.Text = actualHour.ToString();
                if (Value < 0)
                    Value *= -1;

                actualHour = Value;

                if (Value > 23)
                {
                    Value = 23;
                    actualHour = 23;
                    textBox3.Text = "23";
                }

            }
            catch (FormatException)
            {
                if (textBox3.Text != "")
                {
                    if (textBox3.Text.Length > 1)
                        textBox3.Text = actualHour.ToString();
                    else
                    {
                        textBox3.Text = "";
                    }
                }

            }
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                int Value = Convert.ToInt32(textBox1.Text);
                if (textBox1.Text.Contains("+") || textBox1.Text.Contains("-"))
                    textBox1.Text = actualDay.ToString();
                if (Value < 0)
                    Value *= -1;

                actualDay = Value;


            }
            catch (FormatException)
            {
                if (textBox1.Text != "")
                {
                    if (textBox1.Text.Length > 1)
                        textBox1.Text = actualDay.ToString();
                    else
                    {
                        textBox1.Text = "";
                    }
                }

            }
        }

        private void textBox1_LostFocus(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(textBox1.Text))
            {
                textBox1.Text = "0";
                actualDay = 0;
            }
        }

        private void textBox3_LostFocus(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(textBox3.Text))
            {
                textBox3.Text = "0";
                actualHour = 0;
            }
        }

        private void textBox4_LostFocus(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(textBox4.Text))
            {
                textBox4.Text = "0";
                actualMinute = 0;
            }
        }

        private void textBox5_LostFocus(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(textBox5.Text))
            {
                textBox5.Text = "0";
                actualSecond = 0;
            }
        }
    }
}
